﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfUsercontrol.Models;

namespace WpfUsercontrol.UserControls.Combox
{
    /// <summary>
    /// PullDownQueryCombox.xaml 的交互逻辑
    /// </summary>
    public partial class PullDownQueryCombox : UserControl
    {
        public PullDownQueryCombox()
        {
            InitializeComponent();

        }



        public List<ItemModel> ItemList
        {
            get { return (List<ItemModel>)GetValue(ItemListProperty); }
            set { SetValue(ItemListProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemListProperty =
            DependencyProperty.Register("ItemList", typeof(List<ItemModel>), typeof(PullDownQueryCombox), new PropertyMetadata(new List<ItemModel>(), OnListChange));

        private static void OnListChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as PullDownQueryCombox;
            if (control == null || control.SearchableComboBox == null) return;
            control.SearchableComboBox.ItemsSource = control.ItemList;
        }

        public ItemModel CurrentItem
        {
            get { return (ItemModel)GetValue(CurrentItemProperty); }
            set { SetValue(CurrentItemProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CurrentItem.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CurrentItemProperty =
            DependencyProperty.Register("CurrentItem", typeof(ItemModel), typeof(PullDownQueryCombox), new PropertyMetadata(new ItemModel(), OnItemChange));

        private static void OnItemChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as PullDownQueryCombox;
            if (control == null || control.SearchableComboBox == null) return;
            control.SearchableComboBox.SelectedValue = control.CurrentItem.Id;
        }

        public static readonly DependencyProperty PlaceholderProperty =
            DependencyProperty.Register("Placeholder", typeof(string), typeof(PullDownQueryCombox), new PropertyMetadata(string.Empty));

        public string Placeholder
        {
            get => (string)GetValue(PlaceholderProperty);
            set => SetValue(PlaceholderProperty, value);
        }

        private void SearchableComboBox_GotFocus(object sender, RoutedEventArgs e)
        {
            var currentCombox = sender as ComboBox;
            if (currentCombox != null)
            {
                currentCombox.IsDropDownOpen = true;
            }
        }

        private void SearchableComboBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var currentCombox = sender as ComboBox;
            if (currentCombox != null)
            {
                currentCombox.IsDropDownOpen = true;
            }
        }

        private void SearchableComboBox_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            var currentCombox = sender as ComboBox;
            if (currentCombox != null)
            {
                currentCombox.IsDropDownOpen = true;
                // 获取当前输入的文本
                string filterText = currentCombox.Text;
                Load(filterText);

            }
        }

        private void Load(string filterText)
        {
            if (string.IsNullOrEmpty(filterText))
            {
                SearchableComboBox.ItemsSource = ItemList;
            }
            else
            {
                var filterList = ItemList.Where(x => x.Name.Contains(filterText)).ToList();
                SearchableComboBox.ItemsSource = filterList;
            }

        }

        private void SearchableComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var currentCombox = sender as ComboBox;
            if (currentCombox != null)
            {
                if (currentCombox.SelectedItem is ItemModel itemModel)
                {
                    CurrentItem = itemModel;
                }
            }
        }
    }
}
